package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.winterLove.WinterLoveBagItem;
import com.yanqu.road.entity.activity.winterLove.WinterLoveMapAnimal;
import com.yanqu.road.entity.activity.winterLove.WinterLoveMapItem;
import com.yanqu.road.entity.activity.winterLove.WinterLoveUserData;
import com.yanqu.road.entity.activity.winterLove.config.WinterLoveComb;
import com.yanqu.road.entity.activity.winterLove.config.WinterLoveConfig;
import com.yanqu.road.entity.activity.winterLove.config.WinterLoveItem;
import com.yanqu.road.pb.activity.WinterLoveProto;
import com.yanqu.road.server.manger.ServerLanguageMgr;

import java.util.List;

public class WinterLovePb {
    public static WinterLoveProto.CombConfigMsg.Builder parseWinterLoveCombConfigRespMsg(WinterLoveConfig winterLoveConfig, String language) {
        WinterLoveProto.CombConfigMsg.Builder builder = WinterLoveProto.CombConfigMsg.newBuilder();
        for (WinterLoveComb winterLoveComb : winterLoveConfig.getWinterLoveCombList()) {
            WinterLoveProto.CombConfigData.Builder dataBuilder = WinterLoveProto.CombConfigData.newBuilder();
            dataBuilder.setCombId(winterLoveComb.getCellID());
            dataBuilder.setName(ServerLanguageMgr.getContent(winterLoveComb.getName(), language));
            dataBuilder.setType(winterLoveComb.getType());
            dataBuilder.setLevel(winterLoveComb.getLevel());
            dataBuilder.setReward(winterLoveComb.getRewards());
            for (int i : winterLoveComb.getCombination()) {
                dataBuilder.addCombination(i);
            }
            builder.addConfigList(dataBuilder);
        }
        return builder;
    }

    public static WinterLoveProto.ItemConfigMsg.Builder parseWinterLoveItemConfigRespMsg(WinterLoveConfig winterLoveConfig, String language) {
        WinterLoveProto.ItemConfigMsg.Builder builder = WinterLoveProto.ItemConfigMsg.newBuilder();
        for (WinterLoveItem winterLoveItem : winterLoveConfig.getWinterLoveLowItemList()) {
            WinterLoveProto.ItemConfigData.Builder itemBuilder = WinterLoveProto.ItemConfigData.newBuilder();
            itemBuilder.setItemId(winterLoveItem.getItemId());
            itemBuilder.setName(ServerLanguageMgr.getContent(winterLoveItem.getName(), language));
            itemBuilder.setLevel(winterLoveItem.getLevel());
            itemBuilder.setOccupyGridNum(winterLoveItem.getType());
            itemBuilder.setQuality(winterLoveItem.getQuality());
            itemBuilder.setWeight(winterLoveItem.getWeight());
            itemBuilder.setExtraId(winterLoveItem.getExtraId());
            builder.addConfigList(itemBuilder);
        }
        for (WinterLoveItem winterLoveItem : winterLoveConfig.getWinterLoveHighItemList()) {
            WinterLoveProto.ItemConfigData.Builder itemBuilder = WinterLoveProto.ItemConfigData.newBuilder();
            itemBuilder.setItemId(winterLoveItem.getItemId());
            itemBuilder.setName(ServerLanguageMgr.getContent(winterLoveItem.getName(), language));
            itemBuilder.setLevel(winterLoveItem.getLevel());
            itemBuilder.setOccupyGridNum(winterLoveItem.getType());
            itemBuilder.setQuality(winterLoveItem.getQuality());
            itemBuilder.setWeight(winterLoveItem.getWeight());
            itemBuilder.setExtraId(winterLoveItem.getExtraId());
            builder.addConfigList(itemBuilder);
        }
        return builder;
    }

    public static WinterLoveProto.WinterLoveUserDataMsg.Builder parseWinterLoveUserDataRespMsg(WinterLoveUserData userData) {
        WinterLoveProto.WinterLoveUserDataMsg.Builder builder = WinterLoveProto.WinterLoveUserDataMsg.newBuilder();
        builder.setLevel(userData.getLevel());
        if (userData.getLevel() == 1) {
            builder.setTurnNum(userData.getLowTurn());
        } else {
            builder.setTurnNum(userData.getHighTurn());
        }

        List<WinterLoveMapAnimal> winterLoveMapAnimalList = userData.getLevel() == 1 ? userData.getLowAnimalPosition() : userData.getHighAnimalPosition();
        List<WinterLoveMapItem> winterLoveMapItemList = userData.getLevel() == 1 ? userData.getLowRewardMapData() : userData.getHighRewardMapData();
        for (WinterLoveMapAnimal winterLoveMapAnimal : winterLoveMapAnimalList) {
            WinterLoveProto.AnimalData.Builder animalBuilder = WinterLoveProto.AnimalData.newBuilder();
            animalBuilder.setAnimalNum(winterLoveMapAnimal.getAnimalNum());
            animalBuilder.setAnimalType(winterLoveMapAnimal.getAnimalType());
            animalBuilder.addOccupyGrid(winterLoveMapAnimal.getMainDogGrig());
            animalBuilder.addOccupyGrid(winterLoveMapAnimal.getMainDogGrig() + 1);
            animalBuilder.addOccupyGrid(winterLoveMapAnimal.getMainDogGrig() + 6);
            animalBuilder.addOccupyGrid(winterLoveMapAnimal.getMainDogGrig() + 7);
            builder.addAnimalData(animalBuilder);
        }
        List<Integer> winterLoveGridList = userData.getLevel() == 1 ? userData.getLowMapData() : userData.getHighMapData();
        for (int i = 0; i < winterLoveGridList.size(); i++) {
            WinterLoveProto.SnowFieldState.Builder snowBuilder = WinterLoveProto.SnowFieldState.newBuilder();
            snowBuilder.setGridNum(i);
            snowBuilder.setState(winterLoveGridList.get(i));
            builder.addState(snowBuilder);
        }
        for (WinterLoveMapItem winterLoveMapItem : winterLoveMapItemList) {
            boolean isHide = winterLoveMapItem.getOccupyList().stream().mapToInt(x -> x).noneMatch(x -> winterLoveGridList.get(x) == 1);
            WinterLoveProto.MapItemData.Builder mapItemBuilder = WinterLoveProto.MapItemData.newBuilder();
            mapItemBuilder.setItemId(isHide ? 0 : winterLoveMapItem.getDecorationId());
            mapItemBuilder.setDirection(winterLoveMapItem.getRotation());
            mapItemBuilder.setItemNum(winterLoveMapItem.getItemNum());
            mapItemBuilder.setBoxItem(winterLoveMapItem.isBoxItem());
            if (winterLoveMapItem.getInBag() == 0) {
                mapItemBuilder.setInBag(false);
            } else {
                mapItemBuilder.setInBag(true);
            }
            if (!isHide) {
                for (int x : winterLoveMapItem.getOccupyList()) {
                    mapItemBuilder.addOccupyGrids(x);
                }
            }
            builder.addMapItem(mapItemBuilder);
        }
        for (WinterLoveBagItem winterLoveBagItem : userData.getDecoration()) {
            WinterLoveProto.UserItemData.Builder bagItemBuilder = WinterLoveProto.UserItemData.newBuilder();
            bagItemBuilder.setItemId(winterLoveBagItem.getDecorationId());
            bagItemBuilder.setItemNum(winterLoveBagItem.getDecorationNum());
            builder.addUserItem(bagItemBuilder);
        }
        builder.setScore(userData.getScore());
        builder.setBeautyId(userData.getBeautyId());
        builder.setDisperseGoldTimes(userData.getDisperseTimes());
        builder.setBlessNum(userData.getBlessing());

        return builder;
    }

}
